package com.projects.android.views;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.projects.android.R;
import com.projects.android.bus.UsersBUS;
import com.projects.android.utils.SelectedItems;
import com.projects.android.utils.Utils;

public class ChangePassword extends Activity implements OnClickListener {
	private Intent intent;
	private Button btnCancel;
	private Button btnOK;

	private EditText txtOldPassword;
	private EditText txtNewPassword;
	private EditText txtConfirmNewPassword;

	// Use an existing ListAdapter that will map an array
	// of strings to TextViews
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.changepassword);
		initFrm();
	}

	private void initFrm() {
		btnCancel = (Button) findViewById(R.id.btnChangePasswordCancel);
		btnOK = (Button) findViewById(R.id.btnChangePasswordOK);

		txtOldPassword = (EditText) findViewById(R.id.txtChangePasswordOldPassword);
		txtNewPassword = (EditText) findViewById(R.id.txtChangePasswordNewPassword);
		txtConfirmNewPassword = (EditText) findViewById(R.id.txtChangePasswordConfirmNewPassword);

		txtOldPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
		txtNewPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
		txtConfirmNewPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());

		btnOK.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnChangePasswordCancel:
			intent = Utils.browseIntent(this, "Intro");
			startActivity(intent);
			break;
		case R.id.btnChangePasswordOK:
			if (!ValidateUpdate()) {
				return;
			}
			final ProgressDialog dialog = new ProgressDialog(this);
			dialog.setTitle("Loading. Please wait...");
			dialog.setIndeterminate(true);

			final Thread thread = new Thread(new Runnable() {
				@Override
				public void run() {
					runOnUiThread(new Runnable() {
						boolean result = UsersBUS.changePassword(
								SelectedItems.SelectedUsers.getUserName(),
								txtOldPassword.getText().toString(),
								txtNewPassword.getText().toString());
						private AlertDialog dlg;

						@Override
						public void run() {
							dialog.dismiss();
							Log.i("updateAccount result",
									String.valueOf(result));
							if (result) {
								dlg = new AlertDialog.Builder(
										ChangePassword.this)
										.setTitle("Change Password")
										.setMessage(
												"Change password successful")
										.setPositiveButton(
												"OK",
												new DialogInterface.OnClickListener() {
													public void onClick(
															DialogInterface dialog,
															int whichButton) {
														intent = Utils
																.browseIntent(
																		ChangePassword.this,
																		"Intro");
														startActivity(intent);
													}
												}).create();
								dlg.show();
							} else {
								dlg = Utils.showAlertDlg(ChangePassword.this,
										"Change Password",
										"Cannot change password");
								dlg.show();
							}
						}
					});
				}
			});
			dialog.show();
			thread.start();
			break;
		}
	}

	public boolean ValidateUpdate() {
		String errorTxt = "";
		if (txtOldPassword.getText().toString().equals("")) {
			errorTxt = "Old password is empty";
		}
		else if (txtNewPassword.getText().toString().equals("")) {
			errorTxt = "New password is empty";
		}
		else if (txtConfirmNewPassword.getText().toString().equals("")) {
			errorTxt = "Confirm new password is empty";
		}
		else if (!txtNewPassword.getText().toString().equals(txtConfirmNewPassword.getText().toString())) {
			errorTxt = "New password and confirm new password is not match";
		}
		
		if (!errorTxt.equals("")) {
			AlertDialog dlg = Utils.showAlertDlg(this,
					"Change Password", errorTxt);
			dlg.show();
			return false;
		}
		return true;
	}
}
